<?php
namespace My\Controller;

use Utuo\BackgroundController;

class MallGoodsController extends BackgroundController {
	
	public function _initialize() {
		$this->setControllerTitle("商品管理");
		$this->setActionTitle(array(
				'index' => "商品列表",
				'modify' => "编辑商品",
				'setAttribute' => "设置商品属性",
				'add' => "添加商品",
				'recycle' => "回收站"
		));
		$this->setActionNav('index,add,recycle');
	}
	
	public function getLists( $is_deleted ) {
		$page = $this->getPage ();
		$pagesize = $this->getPageSize ();
		$d = D ( 'MallGoods' );
		$params = array();
		$condition = array (
			'cid' => $this->aid,
			'is_deleted' => $is_deleted,
			'cat_id' => $this->catId
		);
		$goodsList = $d->where ( $condition )->limit ( $pagesize )->page ( $page )->order ( 'id DESC' )->select ();
		$goodsCount = $d->where ( $condition )->count ();
		$this->assign ( 'mallGoodsLists', $goodsList );
		$this->showPage ( $goodsCount, $pagesize );
		$this->display();
	}
	
	/**
	 * 添加页
	 */
	public function addAction() {
		$rewardSet = array(
			'0' => '按默认',
			'1' => '自定义',
		);
		$supportCod = array(
			'0' => '否',
			'1' => '是',
		);
		$model = CONTROLLER_NAME;
		$config['supportCodOptions'] =  $this->getOption($supportCod, null, "==请选择==");
		$config['sharingMethodOptions'] = $this->getRadio('reward_set', $rewardSet, 0);
		$config['sharingMethodDealerOptions'] = $this->getRadio('reward_set_dealer', $rewardSet, 0);
		$this->assign(lcfirst($model), $config);
		$this->display('save');
	}
	
	/**
	 * 修改页
	 */
	public function modifyAction() {
		$rewardSet = array(
			'0' => '按默认',
			'1' => '自定义',
		);
		$supportCod = array(
			'0' => '否',
			'1' => '是',
		);
		$model = CONTROLLER_NAME;
		$config = D($model)->getOneById();
		$config['supportCodOptions'] =  $this->getOption($supportCod, $config['support_cod']*1, "==请选择==");
		$config['images'] and $config['images'] = unserialize($config['images']);
		$config['sharingMethodOptions'] = $this->getRadio('reward_set', $rewardSet, $config['reward_set']*1);
		$config['sharingMethodDealerOptions'] = $this->getRadio('reward_set_dealer', $rewardSet, $config['reward_set_dealer']*1);
		$this->assign(lcfirst($model), $config);
		$this->display("save");
	}
	
	public function setAttributeAction() {
		$model = CONTROLLER_NAME;
		$config = D($model)->getOneById();
		if ( !$config['attrs_cfg'] ) {
			$config['attrs_cfg'] = array();
		} else {
			$config['attrs_cfg'] = unserialize($config['attrs_cfg']);
		}
		$attrNames = array_keys($config['attrs_cfg']) or $attrNames = array();
		$this->assign("attrNames", json_encode($attrNames));
		$this->assign("attrJson", json_encode($config['attrs_cfg']));
		$this->assign(lcfirst($model), $config);
		$this->display();
	}
	
	public function setAttributePostAction() { 
		$model = D("MallGoods");
		if ( $model->create() ) {
			!$model->attrs_cfg and $model->attrs_cfg = null;
			if (false !== $model->save()) {
				$this->ajaxSuccess("修改成功！");
			} else {
				$this->ajaxError("修改失败！");
			}
		} else {
			$this->ajaxError($model->getError());
		}
	}
	
	public function setSortAction() {
		$id = I('id', 0, 'intval');
		$sort = I('sort', 0, 'intval');
		M('MallGoods')->where(array('id'=>$id, 'cid'=>$this->aid))->setField('sort', $sort);
	}
	
}
